﻿using System.Collections.Generic;
using NUnit.Framework;
using Nuwanda.Common;
using Nuwanda.TfsSpotlight.Services;
using Nuwanda.TfsSpotlight.Services.Interfaces;
using Nuwanda.TfsSpotlight.Shell.Services.Interfaces;
using Rhino.Mocks;
using Nuwanda.TfsSpotlight.TFS;
using Nuwanda.TfsSpotlight.Data;

namespace TfsSpotlight.Tests.Core.Services
{
    [Author("Bartłomiej Legiędź")]
    [TestFixture]
    public class UserSettingsServiceTests
    {
        private MockRepository _mocks;
        private IIsolatedStorageService _isolatedStorageServiceMock;
        private IVisualStudioServersRegister _visualStudioServersRegisterMock;

        private IUserSettingsService _testSubject;

        [SetUp]
        public void SetUp()
        {
            this._mocks = new MockRepository();
            this._isolatedStorageServiceMock = this._mocks.DynamicMock<IIsolatedStorageService>();
            this._visualStudioServersRegisterMock = this._mocks.DynamicMock<IVisualStudioServersRegister>();

            this._testSubject = new UserSettingsService(this._isolatedStorageServiceMock, this._visualStudioServersRegisterMock);
        }

        [Test]
        public void WhenAskedFor_TfsServers_And_ThereIsNoDuplicates_Should_ReturnExactSameList()
        {
            string[] servers = { "http://wp.pl/", "http://onet.pl/" };

            using (this._mocks.Record())
            {
                Expect.Call(this._visualStudioServersRegisterMock.GetServerNames()).Return(servers);
            }

            List<ServerDescriptor> result;
            using (this._mocks.Playback())
            {
                result = this._testSubject.DiscoverServers();
            }

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("http://wp.pl/", result[0].Url);
            Assert.AreEqual("http://onet.pl/", result[1].Url);
        }

        [Test]
        public void WhenAskedFor_TfsServers_And_ThereAreDuplicates_Should_ReturnDistinctList()
        {
            string[] servers = { "http://wp.pl/", "http://wp.pl/" };

            using (this._mocks.Record())
            {
                Expect.Call(this._visualStudioServersRegisterMock.GetServerNames()).Return(servers);
            }

            List<ServerDescriptor> result;
            using (this._mocks.Playback())
            {
                result = this._testSubject.DiscoverServers();
            }

            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("http://wp.pl/", result[0].Url);
        }
    }
}